package archimede;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.Scanner;
import java.util.StringTokenizer;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextPane;

import net.miginfocom.swing.MigLayout;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

public class Sali extends JPanel {

	private final StartingWindow st;
	private JButton addRilevazione = new JButton(new ImageIcon(
			ClassLoader.getSystemResource("plus.png")));
	private ArrayList<TableSali> values = new ArrayList<TableSali>();
	private int count = 0;

	private JButton upload1 = new JButton(new ImageIcon(
			ClassLoader.getSystemResource("bupload.jpg")));
	private JButton upload2 = new JButton(new ImageIcon(
			ClassLoader.getSystemResource("bupload.jpg")));
	private JButton upload3 = new JButton(new ImageIcon(
			ClassLoader.getSystemResource("bupload.jpg")));
	private JButton upload4 = new JButton(new ImageIcon(
			ClassLoader.getSystemResource("bupload.jpg")));

	private JTextPane note = new JTextPane();
	private String lastPath = "";
	private String pathIm1 = "";
	private String pathIm2 = "";
	private String pathIm3 = "";
	private String pathIm4 = "";
	private String sourceIm1 = "";
	private String sourceIm2 = "";
	private String sourceIm3 = "";
	private String sourceIm4 = "";
	private String sufixIm1 = "";
	private String sufixIm2 = "";
	private String sufixIm3 = "";
	private String sufixIm4 = "";
	private JPanel photo = new JPanel();

	private String directoryProject;

	protected void paintComponent(Graphics g) {
		setOpaque(false);
		g.drawImage(
				(new ImageIcon(ClassLoader.getSystemResource("sfondo.jpg")))
						.getImage(), 0, 0, null);
		super.paintComponent(g);
	}

	public Sali(final StartingWindow st) {
		super();
		this.st = st;

		Properties pc = System.getProperties();
		String homePath = pc.getProperty("user.home");
		directoryProject = homePath + pc.getProperty("file.separator")
				+ "ArchimedeSuper" + pc.getProperty("file.separator")
				+ st.getCount() + pc.getProperty("file.separator");

		setLayout(new MigLayout());

		addRilevazione.setBorder(null);
		addRilevazione.setContentAreaFilled(false);
		addRilevazione.setRolloverEnabled(true);
		addRilevazione.setRolloverIcon(new ImageIcon(ClassLoader
				.getSystemResource("plus_roll.png")));
		RoundedPanel punto = new RoundedPanel("Punto prelievo");
		setCell(punto);
		punto.setPreferredSize(new Dimension(150, 45));
		RoundedPanel cloruri = new RoundedPanel("Sali cloruri");
		setCell(cloruri);
		RoundedPanel nitrati = new RoundedPanel("Sali nitrati");
		setCell(nitrati);
		RoundedPanel solfati = new RoundedPanel("Sali solfati");
		setCell(solfati);

		JLabel noteLabel = new JLabel("Note");
		noteLabel.setFont(new Font("Arial", Font.BOLD, 20));

		ClipboardPopupMenu cbMeteo = ClipboardPopupMenu
				.installForComponent(note);
		cbMeteo.addCopyFunction("Copia");
		cbMeteo.addCutFunction("Taglia");
		cbMeteo.addPasteFunction("Incolla");

		note.setBorder(BorderFactory.createLineBorder(Color.GRAY));
		note.setFont(new Font("Arial", Font.PLAIN, 16));
		note.setPreferredSize(new Dimension(700, 200));
		addRilevazione.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				values.add(new TableSali(Sali.this, count));
				add(values.get(count), "cell 0 " + (1 + ++count)
						+ " 7 1 , gapleft 15");
				// count++;
				if (count < 15)
					add(addRilevazione, "cell 0 " + (2 + count)
							+ " , gapleft 15");
				else
					remove(addRilevazione);
				st.refresh();
			}
		});
		upload1.setBorder(null);
		upload1.setContentAreaFilled(false);
		upload1.setRolloverEnabled(true);
		upload1.setRolloverIcon(new ImageIcon(ClassLoader
				.getSystemResource("upload_icon.jpg")));
		upload1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				ImagePreviewPanel preview = new ImagePreviewPanel();
				chooser.setAccessory(preview);
				chooser.addPropertyChangeListener(preview);
				lastPath = st.getPath();
				if (lastPath.equals(""))
					chooser.getCurrentDirectory();
				else
					chooser.setCurrentDirectory(new File(lastPath));
				chooser.showOpenDialog(upload1);
				String sourcePath = chooser.getSelectedFile().toString();
				st.setPath(sourcePath);
				lastPath = sourcePath;
				StringTokenizer tok = new StringTokenizer(sourcePath, ".");
				String sufix = "";
				while (tok.hasMoreTokens())
					sufix = tok.nextToken();

				sourceIm1 = sourcePath;
				ImageIcon tmp = new ImageIcon(sourcePath);
				tmp.setImage(tmp.getImage().getScaledInstance(250, 200,
						Image.SCALE_AREA_AVERAGING));

				String destPath = directoryProject + "1sali" + "." + sufix;
				pathIm1 = destPath;
				sufixIm1 = sufix;

				upload1.setIcon(tmp);

				photo.add(upload2, "cell 1 0");
				st.refresh();
			}
		});

		upload2.setBorder(null);
		upload2.setContentAreaFilled(false);
		upload2.setRolloverEnabled(true);
		upload2.setRolloverIcon(new ImageIcon(ClassLoader
				.getSystemResource("upload_icon.jpg")));
		upload2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				ImagePreviewPanel preview = new ImagePreviewPanel();
				chooser.setAccessory(preview);
				chooser.addPropertyChangeListener(preview);
				lastPath = st.getPath();
				if (lastPath.equals(""))
					chooser.getCurrentDirectory();
				else
					chooser.setCurrentDirectory(new File(lastPath));
				chooser.showOpenDialog(upload2);
				String sourcePath = chooser.getSelectedFile().toString();
				st.setPath(sourcePath);
				lastPath = sourcePath;
				StringTokenizer tok = new StringTokenizer(sourcePath, ".");
				String sufix = "";
				while (tok.hasMoreTokens())
					sufix = tok.nextToken();

				sourceIm2 = sourcePath;
				ImageIcon tmp = new ImageIcon(sourcePath);
				tmp.setImage(tmp.getImage().getScaledInstance(250, 200,
						Image.SCALE_AREA_AVERAGING));

				String destPath = directoryProject + "2sali" + "." + sufix;
				pathIm2 = destPath;
				sufixIm2 = sufix;

				upload2.setIcon(tmp);

				photo.add(upload3, "cell 2 0");
				st.refresh();
			}
		});

		upload3.setBorder(null);
		upload3.setContentAreaFilled(false);
		upload3.setRolloverEnabled(true);
		upload3.setRolloverIcon(new ImageIcon(ClassLoader
				.getSystemResource("upload_icon.jpg")));
		upload3.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				ImagePreviewPanel preview = new ImagePreviewPanel();
				chooser.setAccessory(preview);
				chooser.addPropertyChangeListener(preview);
				lastPath = st.getPath();
				if (lastPath.equals(""))
					chooser.getCurrentDirectory();
				else
					chooser.setCurrentDirectory(new File(lastPath));
				chooser.showOpenDialog(upload3);
				String sourcePath = chooser.getSelectedFile().toString();
				st.setPath(sourcePath);
				lastPath = sourcePath;
				StringTokenizer tok = new StringTokenizer(sourcePath, ".");
				String sufix = "";
				while (tok.hasMoreTokens())
					sufix = tok.nextToken();

				sourceIm3 = sourcePath;
				ImageIcon tmp = new ImageIcon(sourcePath);
				tmp.setImage(tmp.getImage().getScaledInstance(250, 200,
						Image.SCALE_AREA_AVERAGING));

				String destPath = directoryProject + "3sali" + "." + sufix;
				pathIm3 = destPath;
				sufixIm3 = sufix;

				upload3.setIcon(tmp);
				photo.add(upload4, "cell 3 0");
				st.refresh();
			}
		});

		upload4.setBorder(null);
		upload4.setContentAreaFilled(false);
		upload4.setRolloverEnabled(true);
		upload4.setRolloverIcon(new ImageIcon(ClassLoader
				.getSystemResource("upload_icon.jpg")));
		upload4.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				ImagePreviewPanel preview = new ImagePreviewPanel();
				chooser.setAccessory(preview);
				chooser.addPropertyChangeListener(preview);
				lastPath = st.getPath();
				if (lastPath.equals(""))
					chooser.getCurrentDirectory();
				else
					chooser.setCurrentDirectory(new File(lastPath));
				chooser.showOpenDialog(upload4);
				String sourcePath = chooser.getSelectedFile().toString();
				st.setPath(sourcePath);
				lastPath = sourcePath;
				StringTokenizer tok = new StringTokenizer(sourcePath, ".");
				String sufix = "";
				while (tok.hasMoreTokens())
					sufix = tok.nextToken();

				sourceIm4 = sourcePath;
				ImageIcon tmp = new ImageIcon(sourcePath);
				tmp.setImage(tmp.getImage().getScaledInstance(250, 200,
						Image.SCALE_AREA_AVERAGING));

				String destPath = directoryProject + "4sali" + "." + sufix;
				pathIm4 = destPath;
				sufixIm4 = sufix;

				upload4.setIcon(tmp);
				st.refresh();
			}
		});
		MouseAdapter ml1 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null,
							"Eliminare foto?", "Attenzione", 0);
					if (res == 0) {
						if (!pathIm2.equals("")) {
							pathIm1 = pathIm2.replace("2s", "1s");
							sufixIm1 = sufixIm2;
							sourceIm1 = sourceIm2;
							upload1.setIcon(upload2.getIcon());

							if (!pathIm3.equals("")) {
								pathIm2 = pathIm3.replace("3s", "2s");
								sufixIm2 = sufixIm3;
								sourceIm2 = sourceIm3;
								upload2.setIcon(upload3.getIcon());
								if (!pathIm4.equals("")) {
									pathIm3 = pathIm4.replace("4s", "3s");
									sufixIm3 = sufixIm4;
									sourceIm3 = sourceIm4;
									pathIm4 = "";
									sufixIm4 = "";
									sourceIm4 = "";
									upload3.setIcon(upload4.getIcon());
									upload4.setIcon(new ImageIcon(ClassLoader
											.getSystemResource("bupload.jpg")));
								} else {
									upload3.setIcon(new ImageIcon(ClassLoader
											.getSystemResource("bupload.jpg")));
									pathIm3 = "";
									sufixIm3 = "";
									sourceIm3 = "";
									try {
										photo.remove(upload4);
									} catch (Exception noRemove) {
										System.out.println("no remove");
									}

								}
							} else {
								upload2.setIcon(new ImageIcon(ClassLoader
										.getSystemResource("bupload.jpg")));
								pathIm2 = "";
								sufixIm2 = "";
								sourceIm2 = "";
								try {
									photo.remove(upload3);
								} catch (Exception noRemove) {
									System.out.println("no remove");
								}

							}
						} else {
							pathIm1 = "";
							sufixIm1 = "";
							sourceIm1 = "";
							upload1.setIcon(new ImageIcon(ClassLoader
									.getSystemResource("bupload.jpg")));
							try {
								photo.remove(upload2);
							} catch (Exception noRemove) {
								System.out.println("no remove");
							}
						}
						st.refresh();
					}
				}
			}
		};
		upload1.addMouseListener(ml1);

		MouseAdapter ml2 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null,
							"Eliminare foto?", "Attenzione", 0);
					if (res == 0) {

						if (!pathIm3.equals("")) {
							pathIm2 = pathIm3.replace("3s", "2s");
							sufixIm2 = sufixIm3;
							sourceIm2 = sourceIm3;
							upload2.setIcon(upload3.getIcon());
							if (!pathIm4.equals("")) {
								pathIm3 = pathIm4.replace("4s", "3s");
								sufixIm3 = sufixIm4;
								sourceIm3 = sourceIm4;
								pathIm4 = "";
								sufixIm4 = "";
								sourceIm4 = "";
								upload3.setIcon(upload4.getIcon());
								upload4.setIcon(new ImageIcon(ClassLoader
										.getSystemResource("bupload.jpg")));
							} else {
								upload3.setIcon(new ImageIcon(ClassLoader
										.getSystemResource("bupload.jpg")));
								pathIm3 = "";
								sufixIm3 = "";
								sourceIm3 = "";
								try {
									photo.remove(upload4);
								} catch (Exception noRemove) {
									System.out.println("no remove");
								}
							}
						} else {
							upload2.setIcon(new ImageIcon(ClassLoader
									.getSystemResource("bupload.jpg")));
							pathIm2 = "";
							sufixIm2 = "";
							sourceIm2 = "";
							try {
								photo.remove(upload3);
							} catch (Exception noRemove) {
								System.out.println("no remove");
							}

						}
						st.refresh();
					}
				}
			}
		};
		upload2.addMouseListener(ml2);

		MouseAdapter ml3 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null,
							"Eliminare foto?", "Attenzione", 0);
					if (res == 0) {

						if (!pathIm4.equals("")) {
							pathIm3 = pathIm4.replace("4s", "3s");
							sufixIm3 = sufixIm4;
							sourceIm3 = sourceIm4;
							pathIm4 = "";
							sufixIm4 = "";
							sourceIm4 = "";
							upload3.setIcon(upload4.getIcon());
							upload4.setIcon(new ImageIcon(ClassLoader
									.getSystemResource("bupload.jpg")));
						} else {
							upload3.setIcon(new ImageIcon(ClassLoader
									.getSystemResource("bupload.jpg")));
							pathIm3 = "";
							sufixIm3 = "";
							sourceIm3 = "";
							try {
								photo.remove(upload4);
							} catch (Exception noRemove) {
								System.out.println("no remove");
							}
						}
						st.refresh();
					}

				}
			}
		};
		upload3.addMouseListener(ml3);

		MouseAdapter ml4 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null,
							"Eliminare foto?", "Attenzione", 0);
					if (res == 0) {
						upload4.setIcon(new ImageIcon(ClassLoader
								.getSystemResource("bupload.jpg")));
						pathIm4 = "";
						sufixIm4 = "";
						sourceIm4 = "";
						st.refresh();
					}

				}
			}
		};
		upload4.addMouseListener(ml4);

		photo.setLayout(new MigLayout());
		photo.setOpaque(false);
		photo.add(upload1, "cell 0 0");

		add(photo, "dock north , gapleft 15");
		add(punto, "cell 1 0 , gapleft 10");
		add(cloruri, "cell 2 0 ");
		add(nitrati, "cell 3 0 ");
		add(solfati, "cell 4 0 ");
		add(addRilevazione, "cell 0 1 , gapleft 15");
		add(note, "dock south , gapleft 15");
		add(noteLabel, "dock south , gapleft 15");

	}

	public void setCell(JComponent c) {

		c.setPreferredSize(new Dimension(170, 45));
		c.setOpaque(false);
	}

	public void resize(File in, int w, int h, String formato, String out)
			throws IOException {
		Image newImg = ImageIO.read(in).getScaledInstance(w, h,
				Image.SCALE_SMOOTH);
		BufferedImage bim = new java.awt.image.BufferedImage(w, h,
				BufferedImage.TYPE_INT_RGB);
		bim.createGraphics().drawImage(newImg, 0, 0, null);
		FileOutputStream fos = new FileOutputStream(out);
		ImageIO.write(bim, formato, fos);
		fos.close();
	}

	public void save() {

		try {
			if (!sourceIm1.equals(""))
				resize(new File(sourceIm1), 250, 200, sufixIm1, pathIm1);
			if (!sourceIm2.equals(""))
				resize(new File(sourceIm2), 250, 200, sufixIm2, pathIm2);
			if (!sourceIm3.equals(""))
				resize(new File(sourceIm3), 250, 200, sufixIm3, pathIm3);
			if (!sourceIm4.equals(""))
				resize(new File(sourceIm4), 250, 200, sufixIm4, pathIm4);
		} catch (IOException e) {
			System.out.println("no resize");
			e.printStackTrace();

		}
	}

	public String print() {
		String print = "";
		print = print + "\n#" + count;
		print = print + "\n#" + pathIm1;
		print = print + "\n#" + pathIm2;
		print = print + "\n#" + pathIm3;
		print = print + "\n#" + pathIm4;
		for (int i = 0; i < count; i++) {
			print = print + values.get(i).print();
		}
		print = print + "\n#start";
		print = print + "\n" + note.getText();

		save();
		print = print + "\n@@Sali";

		return print;
	}

	public void setElement(String data) {
		Scanner c = new Scanner(data);
		c.nextLine();
		String prova = c.nextLine().substring(1);
		count = Integer.parseInt(prova);
		pathIm1 = c.nextLine().substring(1);
		if (pathIm1.contains("sali")) {
			pathIm1 = directoryProject + "1sali."
					+ pathIm1.substring(pathIm1.indexOf(".") + 1);
			upload1.setIcon(new ImageIcon(pathIm1));
			photo.add(upload2, "cell 1 0");
		} else
			pathIm1 = "";
		pathIm2 = c.nextLine().substring(1);
		if (pathIm2.contains("sali")) {
			pathIm2 = directoryProject + "2sali."
					+ pathIm2.substring(pathIm2.indexOf(".") + 1);
			upload2.setIcon(new ImageIcon(pathIm2));
			photo.add(upload3, "cell 2 0");
		} else
			pathIm2 = "";
		pathIm3 = c.nextLine().substring(1);
		if (pathIm3.contains("sali")) {
			pathIm3 = directoryProject + "3sali."
					+ pathIm3.substring(pathIm3.indexOf(".") + 1);
			upload3.setIcon(new ImageIcon(pathIm3));
			photo.add(upload4, "cell 3 0");
		} else
			pathIm3 = "";
		pathIm4 = c.nextLine().substring(1);
		if (pathIm4.contains("sali")) {
			pathIm4 = directoryProject + "4sali."
					+ pathIm4.substring(pathIm4.indexOf(".") + 1);
			upload4.setIcon(new ImageIcon(pathIm4));
		} else
			pathIm4 = "";
		String tmp;
		try {
			tmp = c.nextLine();
			while (c.hasNextLine()) {
				tmp = tmp + "\n" + c.nextLine();
			}
		} catch (NoSuchElementException e) {
			tmp = "";
		}
		Scanner scanValue = new Scanner(tmp);
		for (int i = 0; i < count; i++) {
			values.add(new TableSali(Sali.this, i));
			add(values.get(i), "cell 0 " + (1 + i) + " 7 1 , gapleft 15");
			if (count < 15)
				add(addRilevazione, "cell 0 " + (2 + i) + " , gapleft 15");
			else
				remove(addRilevazione);

			String valueTable = scanValue.nextLine().substring(1);
			for (int j = 0; j < 3; j++) {
				valueTable = valueTable + "\n"
						+ scanValue.nextLine().substring(1);
			}
			values.get(i).setElement(valueTable);
		}
		scanValue.nextLine();
		String tp = "";
		while (scanValue.hasNextLine()) {
			tp = tp + "\n" + scanValue.nextLine();
		}
		try {
			note.setText(tp.substring(1));
		} catch (StringIndexOutOfBoundsException e) {
			note.setText("");

		}

		st.refresh();
	}

	public void removeTabel(int line) {
		count--;
		remove(values.get(line));
		values.remove(line);
		for (int i = line; i < count; i++) {
			add(values.get(i), "cell 0 " + (1 + i) + " 7 1");
			values.get(i).setCount(i);
		}

		if (count < 15)
			add(addRilevazione, "cell 0 " + (2 + count));
		else
			remove(addRilevazione);
		st.refresh();
	}

	public Document pdf(Document doc) {

		doc.newPage();
		Chunk underline = new Chunk("7. Analisi tipologia sali",
				FontFactory.getFont(FontFactory.TIMES, 13, Font.BOLD));
		underline.setUnderline(1.2f, -2f); // 0.1 thick, -2 y-location

		try {
			doc.add(underline);
		} catch (DocumentException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

		Paragraph dati = new Paragraph("", FontFactory.getFont(
				FontFactory.TIMES, 11));
		try {
			dati.setSpacingAfter(10);
			dati.setSpacingBefore(10);
			doc.add(dati);
		} catch (DocumentException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

		int col = 0;

		if (!pathIm1.equals(""))
			col++;
		if (!pathIm2.equals(""))
			col++;
		if (!pathIm3.equals(""))
			col++;
		if (!pathIm4.equals(""))
			col++;

		PdfPTable table = new PdfPTable(2);
		table.setWidthPercentage(100);
		if (!pathIm1.equals(""))
			try {
				com.itextpdf.text.Image img = com.itextpdf.text.Image
						.getInstance(pathIm1);
				img.setCompressionLevel(3);
				if (col < 2)
					img.scaleAbsolute(250f, 200f);
				else
					img.scaleAbsolute(235f, 165f);

				PdfPCell cos = new PdfPCell(img);
				if (col == 1)
					cos.setColspan(2);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.setBorder(PdfPCell.NO_BORDER);
				table.addCell(cos);
			} catch (BadElementException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if (!pathIm2.equals(""))
			try {
				com.itextpdf.text.Image img = com.itextpdf.text.Image
						.getInstance(pathIm2);
				img.setCompressionLevel(3);
				if (col < 2)
					img.scaleAbsolute(250f, 200f);
				else
					img.scaleAbsolute(235f, 165f);
				PdfPCell cos = new PdfPCell(img);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.setBorder(PdfPCell.NO_BORDER);
				table.addCell(cos);
			} catch (BadElementException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if (!pathIm3.equals(""))
			try {
				com.itextpdf.text.Image img = com.itextpdf.text.Image
						.getInstance(pathIm3);
				img.setCompressionLevel(3);
				PdfPCell space = new PdfPCell(dati);
				space.setBorder(PdfPCell.NO_BORDER);
				table.addCell(space);
				table.addCell(space);
				if (col < 2)
					img.scaleAbsolute(250f, 200f);
				else
					img.scaleAbsolute(235f, 165f);
				PdfPCell cos = new PdfPCell(img);
				if (col == 3)
					cos.setColspan(2);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.setBorder(PdfPCell.NO_BORDER);
				table.addCell(cos);
			} catch (BadElementException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if (!pathIm4.equals(""))
			try {
				com.itextpdf.text.Image img = com.itextpdf.text.Image
						.getInstance(pathIm4);
				img.setCompressionLevel(3);
				if (col < 2)
					img.scaleAbsolute(250f, 200f);
				else
					img.scaleAbsolute(235f, 165f);
				PdfPCell cos = new PdfPCell(img);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.setBorder(PdfPCell.NO_BORDER);
				table.addCell(cos);
			} catch (BadElementException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		try {
			table.setSpacingAfter(10);
			doc.add(table);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Phrase testo;

		table = new PdfPTable(7);
		int[] size = { 30, 5, 25, 5, 25, 5, 25 };
		try {
			table.setWidths(size);
			table.setWidthPercentage(100);
		} catch (DocumentException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		testo = new Phrase("Materiale analizzato e punti di prelievo",
				FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
		PdfPCell cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setFixedHeight(30);
		table.addCell(cos);

		testo = new Phrase("Sali cloruri", FontFactory.getFont(
				FontFactory.TIMES, 11, Font.BOLD));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setFixedHeight(30);
		cos.setColspan(2);
		table.addCell(cos);

		testo = new Phrase("Sali nitrati", FontFactory.getFont(
				FontFactory.TIMES, 11, Font.BOLD));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setFixedHeight(22);
		cos.setColspan(2);
		table.addCell(cos);

		testo = new Phrase("Sali solfuri", FontFactory.getFont(
				FontFactory.TIMES, 11, Font.BOLD));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setFixedHeight(22);
		cos.setColspan(2);
		table.addCell(cos);

		try {
			doc.add(table);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (int i = 0; i < count; i++) {
			values.get(i).pdf(doc);
		}
		if (!note.getText().equals("")) {
			dati = new Paragraph("Note :\n", FontFactory.getFont(
					FontFactory.TIMES, 13, Font.BOLD));
			try {
				dati.setSpacingBefore(10);
				doc.add(dati);
			} catch (DocumentException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}

			dati = new Paragraph(note.getText(), FontFactory.getFont(
					FontFactory.TIMES, 11));
			try {
				dati.setSpacingAfter(10);
				doc.add(dati);
			} catch (DocumentException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}

		}

		return doc;
	}
}
